import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-12
 * Time: 17:56
 */
public class MyQueue {
    static class ListNode {
        public int value;
        public ListNode next;

        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;
    public ListNode last;
    public int usedSize;

    //1.入队列(链表尾入)
    public void offer(int value) {
        ListNode node = new ListNode(value);
        if (head == null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            last = node;
        }
        usedSize++;
    }

    //2.队列数据个数
    public int getSize() {
        return this.usedSize;
    }

    //3.出队列(头出)
    public int poll() {
        if (head == null) {
            throw new QueueEmptyException("队列为空！");
        }
        int val = head.value;
        if (head.next == null) {
            head = null;
            last = null;
            return val;
        }
        head = head.next;
        this.usedSize--;
        return val;
    }

    //4.获取队头元素
    public int peek() {
        if (head == null) {
            throw new QueueEmptyException("队列为空！");
        }
        return head.value;
    }

    //5.判空
    public boolean empty() {
        return this.usedSize == 0;
    }

    //6.打印
    public void show() {
        ListNode cur = head;
        System.out.print("[ ");
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println("]");
    }
}
